package com.glepoch.springbootsecuritymybatis.compoment.easyexcel;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

/*
 * 性别转换器 0女1男
 * 泛型中的类型为java实体类中的类型
 * */
public class SexConverter implements Converter<Integer> {

    //设置JAVA实体类中的字段的类型
    @Override
    public Class supportJavaTypeKey() {
        return Integer.class;
    }

    //设置excel中的字段类型
    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    //excel转换为java类型
    @Override
    public Integer convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        String data = (String) cellData.getData();
        return "女".equals(data) ? 0 : 1;
    }

    //java转换为excel类型
    @Override
    public CellData convertToExcelData(Integer integer, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {


        return new CellData(integer == 0 ? "女" : "男");
    }
}
